package gui;

import java.awt.Component;
import java.util.Collection;
import java.util.HashMap;

import javax.swing.JTabbedPane;

import data.DepartmentData;
import data.Order;
import data.Order.OrderState;
import data.OrderDepartment;

public class OrderTabbedPane extends JTabbedPane {
	private static final long serialVersionUID = -7877989456814461160L;
	
	private OrderInfoTabPanel mOrderInfoTab;

	public OrderTabbedPane(int type) {
		super(type);
		mOrderInfoTab = new OrderInfoTabPanel(this);
		addTab("Informacje o zam\u00F3wieniu", null, mOrderInfoTab, null);
	}
	
	@Override
	public Component add(Component component) {
		if (component instanceof OrderInfoTabPanel) {
			mOrderInfoTab = (OrderInfoTabPanel)component;
			return mOrderInfoTab;
		}
		else
			return super.add(component);
	}
	
	public OrderInfoTabPanel getOrderInfoTab() {
		return mOrderInfoTab;
	}
	
	public Order createOrder() {
		HashMap<DepartmentData, OrderDepartment> hashMap = new HashMap<>();
		for (int i = 0; i < getComponentCount(); ++i) {
			if (getComponent(i) instanceof ObjectOrderPanel) {
				ObjectOrderPanel temp = (ObjectOrderPanel) getComponent(i);
				hashMap.put(temp.getDepartmentData(), temp.getOrderDepartment());
			}
		}
		return mOrderInfoTab.createOrder(hashMap);
	}

	public void setObjects(Collection<DepartmentData> departments) {
		mOrderInfoTab.setObjects(departments);
	}

	public void changeOrderState(OrderState state) {
		mOrderInfoTab.changeOrderState(state);
	}
	
	public void loadOrder(Order order) {
		mOrderInfoTab.loadOrder(order);
	}

	public void enableEdit(boolean enable) {
		mOrderInfoTab.enableEdit(enable);
		for (Component component : getComponents()) {
			if (component instanceof ObjectOrderPanel)
				((ObjectOrderPanel)component).setEnabled(enable);
		}
	}

	public void resetOrder() {
		mOrderInfoTab.resetOrder();
		for (Component component : getComponents())
			if (component instanceof ObjectOrderPanel)
				remove(component);
	}

	public void saved(int orderId) {
		mOrderInfoTab.setOrderDbId(orderId);
	}
}
